import vue from 'vue'
import Vuex from 'vuex'
import router,{resetRouter} from "../router";
import store from "../store";
import axios from "axios";
vue.use(Vuex)

function addNewRoute(menuList) {
    console.log(menuList)
    let routes = router.options.routes
    console.log(routes)
    routes.forEach(routeItem=>{
        if(routeItem.path=="/Index"){
            menuList.forEach(menu=>{
                let childRoute =  {
                    path:'/'+menu.menuclick,
                    name:menu.menuname,
                    meta:{
                        title:menu.menuname
                    },
                    component:()=>import('../components/'+menu.menucomponent)
                }
                routeItem.children.push(childRoute)
            })
        }
    })

    resetRouter()
    router.addRoutes(routes)
}

export default new Vuex.Store({
    state: {
        menu: [] ,
        userSelectedTime: null, // 存储用户选择的时间
        isChangeTime:false,
        orderSettings: {
            id: 1,
            flashOrderOvertime: 10,
            normalOrderOvertime: 0,
            confirmOvertime: 0,
            finishOvertime: 0,
            commentOvertime: 0
        }
    },
    mutations: {
        setMenu(state, menuList) {
            state.menu = menuList;
            addNewRoute(menuList);
        },
        setUserSelectedTime(state, time) {
            state.userSelectedTime = time;
            sessionStorage.setItem('userSelectedTime', time); // 持久化到本地存储
        },
        setIsChangeTime(state, isChangeTime) {
            state.isChangeTime = isChangeTime;
        },
        incrementTime(state) {
            if (state.userSelectedTime) {
                const newTime = new Date(state.userSelectedTime.getTime() + 1000); // 递增1秒
                state.userSelectedTime = newTime;
                sessionStorage.setItem('userSelectedTime', newTime); // 更新本地存储
            }
        },
        //使用展开运算符（...）来合并现有的 orderSettings 状态和 payload 对象中的新数据。这样，你可以只更新 orderSettings 中的部分字段，而不是替换整个状态对象。
        SET_ORDER_SETTINGS(state, payload) {
            state.orderSettings = { ...state.orderSettings, ...payload };
        }
    },
    actions: {
        updateIsChangeTime({ commit }, isChangeTime) {
            commit('setIsChangeTime', isChangeTime);
        },
        updateUserSelectedTime({ commit }, time) {
            commit('setUserSelectedTime', time);
        },
        incrementTimeAction({ commit }) {
            commit('incrementTime');
        },
        loadUserSelectedTime({ commit }) {
            const savedTime = sessionStorage.getItem('userSelectedTime');
            if (savedTime) {
                commit('setUserSelectedTime', new Date(savedTime));
            }
        },
        fetchOrderSettings({ commit }, httpUrl) {
            axios.get(httpUrl + '/ordersetting/list').then(res => {
                if (res.data.code === 200) {
                    console.log(res.data.data[0]);
                    commit('SET_ORDER_SETTINGS', {
                        normalOrderOvertime: res.data.data[0].normalOrderOvertime,
                        confirmOvertime: res.data.data[0].confirmOvertime,
                        finishOvertime: res.data.data[0].finishOvertime,
                        commentOvertime:res.data.data[0].commentOvertime
                    });
                }else{
                    console.log(res);
                }

            });
        }
    },
    getters: {
        getMenu(state) {
            return state.menu
        },
        getUserSelectedTime(state){
            return state.userSelectedTime
        },
        getIsChangeTime(state) {
            return state.isChangeTime
        }
    }
})
// 在store中初始化时间
store.dispatch('loadUserSelectedTime');
setInterval(() => {
    store.dispatch('incrementTimeAction');
}, 1000);
